package manager.goods;

import java.util.ArrayList;
import java.util.Hashtable;

import common.db.DBExecute;
import common.main.MainBean;

public class GoodsAddBean extends MainBean
{
	public void funcExecute() throws Exception
	{
		String id = (String)inputTable.get("id");
		String client = (String)inputTable.get("client");
		String categorySelect = (String)inputTable.get("categorySelect");
		String goodsName = (String)inputTable.get("goodsName");
		String salePrice = (String)inputTable.get("salePrice");
		String wonga = (String)inputTable.get("wonga");
		String bonus = (String)inputTable.get("bonus");
		String bonusPrice = (String)inputTable.get("bonusPrice");
		String discount = (String)inputTable.get("discount");
		String discountPrice = (String)inputTable.get("discountPrice");
		String jangre = (String)inputTable.get("jangre");
		String selectedGoodsType = (String)inputTable.get("selectedGoodsType");
		String company = (String)inputTable.get("company");
		String selectedPlatform = (String)inputTable.get("selectedPlatform");
		String amount = (String)inputTable.get("amount");
		String info = (String)inputTable.get("info");
		
		if(info.indexOf("'") != -1)
		{
			info = info.replaceAll("'", "''");
		}

		if(goodsName.indexOf("'") != -1)
		{
			goodsName = goodsName.replaceAll("'", "''");
		}
		String seq = getNextSeq();
		inputTable.put("seq", seq);
		
		System.out.println(goodsName);
		

		
		queryBuffer = new StringBuffer("");
		queryBuffer.append("INSERT INTO GOODS (SEQ, NAME, WONGA, PRICE, INFO, GENRE_CODE, TYPE_CODE, ");
		queryBuffer.append("COMPANY_CODE, AMOUNT, BONUS, DISCOUNT_RATE, PLATFORM_CODE, REG_ID, REG_IP, UPD_ID, UPD_IP) ");
		queryBuffer.append("VALUES ");
		queryBuffer.append("('"+seq+"', '"+goodsName+"', '" +wonga+ "', '"+salePrice+ "', '"+info+ "', '"+jangre+ "', '");
		queryBuffer.append(selectedGoodsType+ "', '"+company+ "', '"+amount+ "', '"+bonusPrice+ "', '"+discount+ "', '"+selectedPlatform+ "', '"+id+ "', '"+client+ "', '"+id+ "', '"+client+"')");

		DBExecute dbe = new DBExecute(queryBuffer.toString());
		outputList = dbe.getDataList();
		
		
		GoodsFileManager gfm = new GoodsFileManager();
		int uploadCount = gfm.uploadFile(inputTable);

		inputTable.put("uploadFile", uploadCount+"");
	}
	
	private String getNextSeq() throws Exception
	{
		queryBuffer = new StringBuffer("");
		queryBuffer.append("SELECT GOODS_SEQ.NEXTVAL NEXT_SEQ FROM DUAL");

		DBExecute dbe = new DBExecute(queryBuffer.toString());
		ArrayList seqList =dbe.getDataList();
		Hashtable seqTable = (Hashtable)seqList.get(0);
		return (String)seqTable.get("NEXT_SEQ");
	}
}